#include "../kore2.h"

/**
 * Font constructor, opens a font handle for drawing with.
 *
 * @param char* The face of the font
 * @param int The font size
 */
kore::ui::Font::Font(const char* fontface, int fontsize)
{

	// Create font handle
	m_pFont = TTF_OpenFont(fontface, fontsize);
	if(!m_pFont)
	{
		printf("TTF_OpenFont: %s\n", TTF_GetError());
	}

}

/**
 * Font destructor, cleans up.
 *
 */
kore::ui::Font::~Font()
{

	if (!m_pFont) TTF_CloseFont(m_pFont);

}

/**
 * Draws a font string on the specified surface using the Solid rendering
 * method of SDL_ttf.
 *
 * @see SDL::Font::drawShaded
 * @see SDL::Font::drawBlended
 *
 * @param SDL_Surface* The surface to draw on
 * @param int x coordinate to draw at
 * @param int y coordinate to draw at
 * @param char* The text to output
 * @param SDL_Color The color to draw using
 * @return void
 */
void kore::ui::Font::draw(SDL_Surface* surface, int x, int y, const char* text, SDL_Color color)
{
	SDL_Surface* pfont;
	pfont = TTF_RenderUTF8_Solid(m_pFont,text,color);

	if (pfont)
	{
		// We only need X and Y to blit the surface
		SDL_Rect dest = { x, y, 0, 0 };
		SDL_BlitSurface( pfont, NULL, surface, &dest );
		// And make sure to free the surface afterwards
		SDL_FreeSurface( pfont );
	}
}

/**
 * Draws a font string on the specified surface using the Shaded rendering
 * method of SDL_ttf. This gives better quality than Solid but is a bit more
 * intensive. It renders against a solid background color.
 *
 * @see SDL::Font::draw
 * @see SDL::Font::drawBlended
 *
 * @param SDL_Surface* The surface to draw on
 * @param int x coordinate to draw at
 * @param int y coordinate to draw at
 * @param char* The text to output
 * @param SDL_Color The color to draw using
 * @param SDL_Color The background color to shade to
 * @return void
 */
void kore::ui::Font::drawShaded(SDL_Surface* surface, int x, int y, const char* text, SDL_Color color, SDL_Color bgcolor)
{
	SDL_Surface* pfont;
	pfont = TTF_RenderUTF8_Shaded(m_pFont,text,color,bgcolor);

	if (pfont)
	{
		// We only need X and Y to blit the surface
		SDL_Rect dest = { x, y, 0, 0 };
		SDL_BlitSurface( pfont, NULL, surface, &dest );
		// And make sure to free the surface afterwards
		SDL_FreeSurface( pfont );
	}
}

/**
 * Draws a font string on the specified surface using the Blended rendering
 * method of SDL_ttf. This gives better quality than Solid but is a bit more
 * intensive. It looks very good tho!
 *
 * @see SDL::Font::draw
 * @see SDL::Font::drawShaded
 *
 * @param SDL_Surface* The surface to draw on
 * @param int x coordinate to draw at
 * @param int y coordinate to draw at
 * @param char* The text to output
 * @param SDL_Color The color to draw using
 * @return void
 */
void kore::ui::Font::drawBlended(SDL_Surface* surface, int x, int y, const char* text, SDL_Color color)
{
	SDL_Surface* pfont;
	pfont = TTF_RenderUTF8_Blended(m_pFont,text,color);

	if (pfont)
	{
		// We only need X and Y to blit the surface
		SDL_Rect dest = { x, y, 0, 0 };
		SDL_BlitSurface( pfont, NULL, surface, &dest );
		// And make sure to free the surface afterwards
		SDL_FreeSurface( pfont );
	}
}

